<template>
  <div id='shoptype'>
    <v-titletop>
      <span
        class='titleh'>父级分类的ID:</span>
      <el-input
        placeholder="请输入内容"
        v-model='findId'>
      </el-input>
      <el-button
        type="primary"
        @click='find'>查找
      </el-button>
    </v-titletop>
    <v-titlebottom>
      <el-button
        type="primary"
        @click='add'>
        添加分类
      </el-button>
      <span class='title'
        @click='goback'>{{title}}</span>
      <template style='auto'>
        <el-table
          :data="categorylist"
          height="auto" border
          style="width: 98.5%">
          <el-table-column
            label="父级分类ID"
            prop="parentId"
            width="180">
          </el-table-column>
          <el-table-column
            label="名称"
            prop='name'
            width="300">
          </el-table-column>
          <el-table-column
            label="操作">
            <template
              slot-scope="scope">
              <el-button
                size="mini"
                @click="handlego(scope.$index, scope.row)">
                查看子分类
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </v-titlebottom>
    <keep-alive>
      <component :is='comname'
        v-if='flag'
        @func='send'
        @get='getcategorylist'
        :parentid='parentid'
        :cominfo='cominfo'>
      </component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: 'shoptype',
  components: {
    'v-titletop': () => import('@/components/title-top'),
    'v-titlebottom': () => import('@/components/title-bottom'),
    addshoptype: () => import('@/components/commons/addshoptype'),
  },
  data() {
    return {
      categorylist: [],
      parentid: 0,
      title: '一级分类',
      cominfo: '',
      flag: false,
      comname: '',
      id: '',
      findId: '',
    }
  },
  created() {
    this.getcategorylist()
  },
  methods: {
    getcategorylist() {
      this.$axios({
        method: 'get',
        url: '/manage/category/list',
        params: {
          parentId: this.parentid,
        },
      }).then((res) => {
        if (res.data.data.length || this.id == 0) {
          this.categorylist = res.data.data
        } else {
          this.$message.error('不存在子分类')
        }
      })
    },
    handlego(index, row) {
      this.parentid = row._id
      this.id = row.parentId
      this.title = '二级分类(返回)'
      this.getcategorylist()
    },
    add() {
      this.flag = true
      this.cominfo = '添加商品分类'
      this.comname = 'addshoptype'
    },
    send(data) {
      this.flag = data
    },
    goback() {
      this.title = '一级分类'
      this.parentid = 0
      this.getcategorylist()
    },
    find() {
      if (this.findId) {
        this.$axios({
          method: 'post',
          url: '/manage/category/info',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          params: {
            categoryId: this.findId,
          },
        }).then((res) => {
          if (res.data.status) {
            this.$alert('没找到该分类', '提示')
          } else {
            this.categorylist = []
            this.categorylist.push(res.data.data)
            console.log(res.data.data)
          }
          this.findId = ''
        })
      } else {
        this.getcategorylist()
      }
    },
  },
}
</script>

<style lang='less'>
#shoptype {
  padding: 10px;
  box-sizing: border-box;
  .title {
    width: 100px;
    height: 30px;
    margin-left: 30px;
    font-weight: 700;
    cursor: pointer;
    vertical-align: middle;
  }
  .el-table__row {
    .cell {
      .el-button {
        width: 100px;
        height: 30px;
      }
    }
  }
}
</style>